import { Request, Response } from 'express';
import IResponse from '@/utils/IResponse';

let access = false;

const getAccess = () => {
  return access;
};

/** 延时 */
const waitTime = async (time: number = 100) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(true);
    }, time);
  });
};

/** 随机生成boolean类型 */
const bool = (max: number, min: number) => {
  let range = max - min;
  let rand = Math.random();
  return min + Math.round(rand * range) !== min;
};

let user: API.SessionUser = {
  id: 'cc53299a-1ea3-42cf-acc4-7e3439e9834c',
  username: 'user',
  valid: true,
  createBy: 'system',
  createDate: '2022-01-01 00:00:00',
  updateBy: 'system',
  updateDate: '2022-01-01 00:00:00',
};

let admin: API.SessionUser = {
  id: '16402e9d-a5f9-432e-acf4-fd1f35dfccab',
  username: 'admin',
  valid: true,
  createBy: 'system',
  createDate: '2022-01-01 00:00:00',
  updateBy: 'system',
  updateDate: '2022-01-01 00:00:00',
};

let userInfoEntity: API.UserInfoEntity = {
  id: 'cc53299a-1ea3-42cf-acc4-7e3439e9834c',
  username: 'user',
  password: '',
  valid: true,
  createBy: 'system',
  createDate: '2022-01-01 00:00:00',
  updateBy: 'system',
  updateDate: '2022-01-01 00:00:00',
};

let currentUser = user;

const getCurrentUser = () => {
  return currentUser;
};

export default {
  'POST /api/users/login': async (req: Request, res: Response) => {
    await waitTime(1000);
    const { username, password, verify } = req.body;

    let response_403: IResponse = {
      success: false,
      errorCode: '403',
      errorMessage: '验证失败',
    };
    let response_404: IResponse = {
      success: false,
      errorCode: '404',
      errorMessage: '未找到任何信息',
    };
    let response_405: IResponse = {
      success: false,
      errorCode: '405',
      errorMessage: '验证码校验失败',
    };
    let response_500: IResponse = {
      success: false,
      errorCode: '500',
      errorMessage: '服务器内部错误',
    };

    if (username === admin.username && password === 'admin' && verify) {
      res.status(200).send({
        success: true,
        data: admin,
      });
      currentUser = admin;
      access = true;
      return;
    } else if (username === user.username && password === 'user' && verify) {
      res.status(200).send({
        success: true,
        data: user,
      });
      access = true;
      return;
    } else if (username === '403' && password === '403') {
      res.send(response_403);
      return;
    } else if (username === '405' && password === '405') {
      res.send(response_405);
      return;
    } else if (username === '404' && password === '404') {
      res.send(response_404);
      return;
    } else {
      res.send(response_500);
      return;
    }
  },
  '/api/users/logout': async (req: Request, res: Response) => {
    await waitTime(1000);
    if (getAccess()) {
      access = false;
      let response_201: IResponse = {
        success: true,
        data: {
          username: '201',
        },
      };
      res.status(200).send(response_201);
      return;
    }
    let response_500: IResponse = {
      success: true,
      errorCode: '500',
      errorMessage: '服务器内部错误',
    };
    res.status(500).send(response_500);
    return;
  },
  '/api/users/current': async (req: Request, res: Response) => {
    await waitTime(1000);
    if (getAccess()) {
      res.status(200).send({
        success: true,
        data: getCurrentUser(),
      });
      return;
    }
    let response_401: IResponse = {
      success: true,
      errorCode: '401',
      errorMessage: '请先登录！',
    };
    res.status(401).send(response_401);
    return;
  },
  'PUT /api/users/:id': async (req: Request, res: Response) => {
    await waitTime(1000);
    if (getAccess()) {
      let id = req.params.id;
      if (!id) {
        res.status(405).send({
          success: true,
          errorCode: '405',
          errorMessage: 'ID不能为空',
        });
        return;
      }

      if (id !== currentUser.id) {
        res.status(200).send({
          success: true,
          data: userInfoEntity,
        });
        return;
      }

      res.status(401).send({
        success: true,
        errorCode: '404',
        errorMessage: '所更新的数据不存在',
      });
      return;
    }
  },
};
